package sqlwriter.utils;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class UFile
{
	public static File create(String filename)
	{
		File file = new File(filename);

		if (file.exists() == false)
		{
			file.mkdirs();
		}

		return file;
	}

	public static void delete(String filename)
	{
		delete(new File(filename));
	}
	
	public static void delete(File file)
	{
		File[] files = file.listFiles();
		if (files != null)
		{
			for (File f : files)
			{
				if (f.isDirectory())
				{
					delete(f);
				}
				else
				{
					f.delete();
				}
			}
		}
		
		file.delete();
	}
	
	public static boolean write(String filename, String content, boolean append)
	{
		FileWriter out = null;
		BufferedWriter writer = null;

		try
		{
			out = new FileWriter(filename, append);
			writer = new BufferedWriter(out);
			writer.write(content, 0, content.length());
		}
		catch (IOException e)
		{
			return false;
		}
		finally
		{
			try
			{
				if (writer != null)
					writer.close();

				if (out != null)
					out.close();
			}
			catch (IOException e)
			{
			}
		}

		return true;
	}

}
